package com.loong.musicdemo.ui.find.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.loong.musicdemo.R;
import com.loong.musicdemo.model.Singer;
import com.loong.musicdemo.ui.find.activity.ShowSingerDetailActivity;

import java.util.List;
/*
    歌手adapter处理类
 */
public class SingerAdapter extends RecyclerView.Adapter<SingerAdapter.ViewHolder> {
    private List<Singer> singerList;
    private Context context;

    public SingerAdapter(List<Singer> singerList, Context context) {
        this.singerList = singerList;
        this.context = context;
    }


    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.search_singer_item,parent,false);
        ViewHolder holder = new ViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        Singer singer = singerList.get(position);
        holder.search_singer_name.setText(singer.getName());
        Glide.with(context).load(singer.getImg1v1Url()).skipMemoryCache(true).diskCacheStrategy(DiskCacheStrategy.NONE).into(holder.search_singer_image);
        holder.view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int pos = holder.getAdapterPosition();
                Singer s = singerList.get(pos);
                Intent intent = new Intent(context, ShowSingerDetailActivity.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//                intent.putExtra("singerId",s.getId());
                intent.putExtra("singerAccountId",s.getAccountId());
                intent.putExtra("singerBg",s.getBackgroundUrl());
                context.startActivity(intent);
            }
        });
    }

    @Override
    public int getItemCount() {
        return singerList.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        ImageView search_singer_image;
        TextView search_singer_name;
        View view;
        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            view = itemView;
            search_singer_image = itemView.findViewById(R.id.search_singer_image);
            search_singer_name = itemView.findViewById(R.id.search_singer_name);
        }
    }
}
